<template>
  <div>
    <div class="crumbs">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item>
          <i class="el-icon-lx-cascades"></i> 类型信息
        </el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <div class="container">
      <div class="handle-box">
        <el-button
            type="primary"
            icon="el-icon-delete"
            class="handle-del mr10"
            @click="delAllSelection"
        >批量删除</el-button>
        <el-input v-model='query.tname'  placeholder="类型名" class="handle-input mr10"></el-input>
        <el-button type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button>
          <el-button type="primary" icon="el-icon-plus" @click="addType">添加类型</el-button>
      </div>
      <el-table
          :data="tableData"
          border
          class="table"
          ref="multipleTable"
          header-cell-class-name="table-header"
          @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" align="center"></el-table-column>
        <el-table-column prop="tid" label="类型编号" width="55" align="center"></el-table-column>
        <el-table-column prop="tname" label="类型名"></el-table-column>
          <el-table-column label="操作" width="180" align="center">
          <template #default="scope">
            <el-button
                type="text"
                icon="el-icon-edit"
                @click="handleEdit(scope.$index, JSON.parse(JSON.stringify(scope.row)))"
            >编辑
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination">
<!--        // layout prev 左箭头 pager 页数 next 右箭头
        //@current-change 页数改变时间 有一个参数 就是当前页数
        //total 条数 会根据总条数和每页显示的条数进行 分页-->
        <el-pagination
            background
            layout="prev, pager, next"
            @current-change="preNext"
            :page-size='pagesize'
            :total="total">
        </el-pagination>
      </div>
    </div>

    <!-- 编辑弹出框 -->
    <el-dialog :title='title' :visible.sync='editVisible' width="30%">
      <el-form ref="form" :model="form" label-width="70px">
        <el-form-item label="类型名">
          <el-input v-model="form.tname"></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
                <span class="dialog-footer">
                    <el-button @click="editVisible = false">取 消</el-button>
                    <el-button type="primary" @click="submit">确 定</el-button>
                </span>
      </template>
    </el-dialog>
  </div>
</template>

<script>

export default {
  name: "typetable",
  data() {
    return {
      query: {
        pageNum: 1,
        pageSize: 10
      },
      tableData: [],
      multipleSelection: [],
      delList: [],
      editVisible: false,
      total: 6,
        pagesize:5,
      form: {},
      idx: -1,
      id: -1,
        title:[],
    };
  },
  created() {
    this.getData();
  },
  methods: {
    // 获取 easy-mock 的模拟数据
    getData() {
        //类型列表
     this.$http({
       url:'/type/selectType',
       method:'post',
       params:{pageNum:1,pageSize:5,tname:this.query.tname}
     }).then(response=>{
         //console.log(response)
       //console.log(response)
       this.tableData=response.list
         this.total=response.total
         this.pagesize=response.pageSize
     })
    },
    // 触发搜索按钮
    handleSearch() {
      this.$http({
        url:'/type/selectType',
        method:'post',
        params:{pageNum:1,pageSize:5,tname:this.query.tname}
      }).then(response=>{
          this.total=response.total
          this.pagesize=response.pageSize
        this.tableData=response.list
      })
        //添加
    },addType(){
          this.title='添加专类型'
          this.form=[];
          this.editVisible=true;
      }
    ,
    // 删除操作
    handleDelete(index) {
      // 二次确认删除
      this.$confirm("确定要删除吗？", "提示", {
        type: "warning"
      })
          .then(() => {
            this.$message.success("删除成功");
            this.tableData.splice(index, 1);
          })
          .catch(() => {});
    },
      // 多选操作
      handleSelectionChange(val) {
          this.multipleSelection = val;
      },
    delAllSelection() {
        let str="";
        for (let i=0;i<this.multipleSelection.length;i++){
            str+=this.multipleSelection[i].tid+","
        }
        this.$http({
            url:'/type/delType',
            method:'post',
            params:{str:str}
        }).then(res=>{
            if(res==true){
                this.$message.success('删除成功')
                this.getData()
            }else {
                this.$message.error('删除失败该类型下存在专辑')
            }

        })
    },
    // 编辑操作
    handleEdit(index, row) {
        //文件上传传递的参数
        this.title='编辑';
      this.idx = index;
      this.form = row;
      this.editVisible = true;
    },
    // 保存编辑
    saveEdit() {
    },
      submit(){//动态提交
        if(this.form.length==0){
            this.$http({
                url:'/type/typeAdd',
                method:'post',
                params:{tname:this.form.tname }
            }).then(response=>{
                if(response==true){
                    this.$message.success('添加成功')
                    this.editVisible = false;
                    this.getData()
                }else {
                    this.$message.error('添加失败')
                }
            })
        }else {
            this.$http({
                url:'/type/updType',
                method:'post',
                params:{tid:this.form.tid,tname:this.form.tname
                }
            }).then(res=>{
                if(res===true){
                    this.$message.success("修改成功")
                    this.getData()
                }else {
                    this.$message.warning("修改失败")
                }
            })
            this.editVisible = false;
        }
      },
    // 分页导航
    handlePageChange() {
      this.getData();
    },preNext(val){
      this.$http({
        url:'/type/selectType',
        method:'post',
        params:{pageNum:val,pageSize:5,aname:this.query.tname}
      }).then(response=>{
          this.total=response.total
          this.pagesize=response.pageSize
        this.tableData=response.list
      })

    }
  }
};
</script>

<style scoped>
.handle-box {
  margin-bottom: 20px;
}

.handle-select {
  width: 120px;
}

.handle-input {
  width: 300px;
  display: inline-block;
}
.table {
  width: 100%;
  font-size: 14px;
}
.red {
  color: #ff0000;
}
.mr10 {
  margin-right: 10px;
}
.table-td-thumb {
  display: block;
  margin: auto;
  width: 40px;
  height: 40px;
}
</style>
